<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品列表页</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css"
          integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <link rel="stylesheet" href="css/goodsList.css">
    <script src="js/jquery-3.3.1.min.js"></script>
     <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
            integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
            crossorigin="anonymous"></script> 
</head>
<body>
<div class="nav-wrapper">
    <nav class="navbar navbar-default navbar-inverse navbar-fixed-top">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                        data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">XX购物平台</a>
            </div>
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="goodsList.html">首页<span class="sr-only">(current)</span></a></li>
                    <!-- <li><a href="login.html">登陆</a></li>
                    <li><a href="register.html">注册</a></li> -->
                    <li style="color:white;">${user==null?"游客":user.uname }</li>
                    <li><a href="#">收藏</a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                           aria-expanded="false">Dropdown <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">Action</a></li>
                            <li><a href="#">Another action</a></li>
                            <li><a href="#">Something else here</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="#">Separated link</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="#">One more separated link</a></li>
                        </ul>
                    </li>
                </ul>
                <form class="navbar-form navbar-left">
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="请输入内容">
                    </div>
                    <button type="submit" class="btn btn-default">搜索</button>
                </form>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="goodsCart.html">购物车</a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                           aria-expanded="false">联系客服 <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">帮助中心</a></li>
                            <li><a href="#">售后服务</a></li>
                            <li><a href="#">Something else here</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="#">Separated link</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
</div>
<div class="list-wrapper">
 <c:forEach items="${page.data }" var="goods">
    <div class="list-goods">
        <div class="goods-img">
           <a href="find.goods?id=${goods.gid}"> <img src="./img/${goods.gimg }" style="width:200px;height:100px" alt=""></a>
        </div>
        <div class="goods-in">
            <div class="goods-name">${goods.gid }&nbsp;&nbsp;${goods.gname }</div>
            <div class="goods-price">${goods.gprice }</div>
            <div class="goods-info">${goods.gdepict}</div>
            <div>  <input type="text" name="gnumber">             
            </div>
            <div class="button-wrapper">
                <button class="btn btn-default" type="button" onclick="fun(this,${goods.gid})"><div class="glyphicon glyphicon-plus"></div>&nbsp;&nbsp;购物车</button>
                <button class="btn btn-default" type="submit"><div class="glyphicon glyphicon-heart-empty"></div></button>
            </div>
        </div>
    </div>
    </c:forEach>
  


</div>

<div>
  <button onclick="last()">上一页</button><button onclick="next()">下一页</button>
</div>
<script type="text/javascript">
   function last(){
	   var currentpage=${page.currentpage};
	   if(currentpage==1){
		   window.location.href="list.goods?n="+1;
	   }else{
		   window.location.href="list.goods?n="+(currentpage-1);
	   }
   }
   function next(){
	   var endpage=${page.pagesum};
	   var currentpage=${page.currentpage};
	   if(currentpage==endpage){
		   window.location.href="list.goods?n="+endpage;
	   }else{
		   window.location.href="list.goods?n="+(currentpage+1);
	   }
   }


/* 
jQuery.parent(expr)           //找父元素

jQuery.parents(expr)          //找到所有祖先元素，不限于父元素

jQuery.children(expr)        //查找所有子元素，只会找到直接的孩子节点，不会返回所有子孙

jQuery.contents()            //查找下面的所有内容，包括节点和文本。

jQuery.prev()                //查找上一个兄弟节点，不是所有的兄弟节点

jQuery.prevAll()             //查找所有之前的兄弟节点

jQuery.next()                //查找下一个兄弟节点，不是所有的兄弟节点

jQuery.nextAll()             //查找所有之后的兄弟节点
*
*/


  function fun(but,gid){
	var gnumber=$(but).parent().prev().children().eq(0).val();
	
	 
	  /* console.log(gnumber); */
	   $.ajax({
		   url:"add.cart",
		   data:{gid:gid,gnumber:gnumber},
		   async:false,
		   dataType:"json",
		   success:function(result){
			  
			   if(result.flag==false){
				   alert("购物车添加失败");
				   
			   }else{
				   alert("购物车添加成功");
						   
			   }
		   }
	   }) 
	   
	   
  }
  </script>
</body>
</html>